<?php
/**
 * 
 */
define('SOCKET_SERVER_DEBUG', false);
error_reporting((SOCKET_SERVER_DEBUG ? 32767 : 0));
ini_set('default_socket_timeout', -1);
new cli_socket();
class cli_socket 
{
	

	function __construct($arg)
	{
		$filename = Q::ini('app_config/ROOT_DIR').'/config/app.yaml';
	    $appConfig = Helper_YAML::load($filename); 
	 
	    $this->socket_server = $this->appConfig['wxapikey'];
	    if (!(class_exists('swoole_websocket_server')))
        {
            return $this->error('Swoole扩展未安装或服务未启动');
        }
        $config = array('worker_num' => 3, 'daemonize' => 0);
        $this->server = new swoole_websocket_server("127.0.0.1", 9309,SWOOLE_BASE);
        $this->server->set($config);
        $this->server->on('open', function (swoole_websocket_server $server, $request) {
            echo "server: handshake success with fd{$request->fd}\n";
        });
        $this->server->on('message', function (swoole_websocket_server $server, $frame) {
            echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
            $data = json_decode($frame->data,true);
            $message=$this->msg($data['video_id'],$data['id'],$data['acct_id']);//查询刚添加的数据，推送给用户
            if($message['code']==0){
                //推送消息
                foreach ($this->server->connections as $fd) {
                    $this->server->push($fd, json_encode($message['data'],JSON_UNESCAPED_UNICODE));
                }
            }

        });
        $this->server->on('Start', function()
        {
            swoole_set_process_name('EDULIVE');
        });
        $this->server->on('close', function ($ser, $fd) {
            echo "client {$fd} closed\n";
        });

        $this->server->start();
	}

	public function msg($video_id,$acct_id){
        $sql="select c.content,c.id,c.upload_img,c.addtime,u.avatar_url,sum(case when ct.is_delete = 0 and ct.type = 0 then 1 else 0 end) as thump_count,u.nickname from edu_comment as c left join edu_video_user as u on u.id=c.user_id where c.acct_id={$acct_id} and c.id={$maxid} and c.is_delete=0 and c.c_id=0 and top_id=0 and video_id={$video_id}";
        $list_first=QDB::getConn()->execute($sql)->fetchAll();

        if(!empty($list_first)){
            foreach($list_first as $index=>$value) {
                $list_first[$index]['img'] = json_decode($value['upload_img'], true);
                $list_first[$index]['children'] = [];
                $list_first[$index]['content'] = $this->userTextDecode($value['content']);
            }
            return [
                'code'=>0,
                'msg'=>'success',
                'data'=>[
                    'list'=>$list_first,
                    'video_id'=>$video_id
                ]
            ];
        }else{
            return [
                'code'=>1,
                'msg'=>'没有数据',
            ];
        }
    }

    public  function userTextDecode($str){
        $text = json_encode($str); //暴露出unicode
        $text = preg_replace_callback('/\\\\\\\\/i',function($str){
            return '\\';
        },$text); //将两条斜杠变成一条，其他不动
        return json_decode($text);
    }

}